$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/classtimeteacher/list',
        datatype: "json",
        colModel: [			
			// { label: 'id', name: 'id', index: 'id', width: 50, key: true },
			// { label: '课程id', name: 'courseid', index: 'courseid', width: 80 },
            { label: '课程', name: 'courseid', index: 'courseid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var username = "" ;
                    if (null!=rowdata.courseid) {
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/course/info/"+rowdata.courseid,
                            success: function(r){
                                username = r.course.title;

                            }
                        });
                    }
                    return username;
                }
            },
            // { label: '课次id', name: 'classtimeid', index: 'classtimeid', width: 80 },
            { label: '课次', name: 'classtimeid', index: 'classtimeid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var name = "" ;
                    if (null!=rowdata.classtimeid) {
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/classtime/info/"+rowdata.classtimeid,
                            success: function(r){
                                if (null!=r.classtime) {
                                    name = r.classtime.name;
                                } else {
                                    name= ""
                                }
                            }
                        });
                    }
                    return name;
                }
            },
			// { label: '老师id', name: 'userid', index: 'userid', width: 80 },
            { label: '老师', name: 'userid', index: 'userid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var name = "" ;
                    if (null!=rowdata.userid) {
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/users/info/"+rowdata.userid,
                            success: function(r){
                                if (null!=r.users) {
                                    name = r.users.username;
                                } else {
                                    name= ""
                                }
                            }
                        });
                    }
                    return name;
                }
            },
            { label: '视频路径', name: 'vediourl', index: 'vediourl', width: 80 },
			{ label: '题目解析', name: 'topicanalysis', index: 'topicAnalysis', width: 80 },
			{ label: '上传时间', name: 'answertime', index: 'answertime', width: 80 }, 			

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        classtimes: {},
        courses: {},
        users: {},
		classtimeteacher: {
            classname:null,
            coursename:null,
            username:null
        }
	},
    classtimes: {},
    courses: {},
    users: {},
    classtimeteacher: {
        classname:null,
        coursename:null,
        username:null
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            vm.getCourses();
            vm.getUsers();
            vm.getClasstimes();
			vm.showList = false;
			vm.title = "新增";
			vm.classtimeteacher = {
                courseid: null,
                classtimeid: null,
                userid : null
            };
		},
		update: function (event) {
            vm.getCourses();
            vm.getUsers();
            vm.getClasstimes();
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.classtimeteacher.id == null ? "sys/classtimeteacher/save" : "sys/classtimeteacher/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.classtimeteacher),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/classtimeteacher/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/classtimeteacher/info/"+id, function(r){
                vm.classtimeteacher = r.classtimeteacher;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        getClasstimes: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/classtime/getClasstimes",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.classtimes = r.data;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setClassTimeTitle: function (id) {
            for (let i = 0; i < vm.classtimes.length; i++) {
                if (id == vm.classtimes[i].id) {
                    vm.classtimeteacher.name = vm.classtimes[i].id;
                }
            }
            console.log(vm);
        },
        getCourses: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/course/getCourses",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.courses = r.data;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setCourseTitle: function (id) {
            for (let i = 0; i < vm.courses.length; i++) {
                if (id == vm.courses[i].id) {
                    vm.classtimeteacher.coursename = vm.courses[i].title;
                }
            }
            console.log(vm);
        },
        getUsers: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/users/getUsers",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.users = r.data;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setUsername: function (id) {
            for (let i = 0; i < vm.users.length; i++) {
                if (id == vm.users[i].id) {
                    vm.classtimeteacher.username = vm.users[i].username;
                }
            }
            console.log(vm);
        },
	}
});